<?php
/**
 * Created by JetBrains PhpStorm.
 * User: dodasso
 * Date: 7/14/13
 * Time: 8:10 PM
 * To change this template use File | Settings | File Templates.
 */

define('APP_ROOT', realpath(__DIR__ . '/../..'));

require_once(APP_ROOT . '/modules/common/Errors.php');
require_once(APP_ROOT . '/modules/persistance/DBConnector.php');
require_once(APP_ROOT . '/modules/session/SessionModule.php');

use common\Errors;
use persistance\DBConnector;
use session\SessionModule;

function testLoginSuccess() {
    $dbconn = new DBConnector();
    $dbconn->connect();
    $mod = new SessionModule();
    if(!$mod->init($dbconn)) {
        print('testLoginSuccess(): FAIL' . PHP_EOL);
        return;
    };

    $rs = $mod->login('admin', '73e1ddfb1e4c7ef386a0a9bcc6be34f0eec43dc5', 'localhost', 'none');
    $json = json_decode($rs, true);
    if(is_null($json) || ($json['errorCode'] !== Errors::INTEGRAL_ERR_NONE)) {
        print('testLoginSuccess(): FAIL' . PHP_EOL);
    } else {
        print('testLoginSuccess(): OK' . PHP_EOL);
    }
    $mod->logout($json['data']['sessionKey']);
}

function testLoginFail() {
    $dbconn = new DBConnector();
    $dbconn->connect();
    $mod = new SessionModule();
    if(!$mod->init($dbconn)) {
        print('testLoginFail(): FAIL' . PHP_EOL);
        return;
    };

    $rs = $mod->login('admin', 'admin2', 'localhost', 'none');
    $json = json_decode($rs, true);
    if(is_null($json)) {
        print('testLoginFail(): FAIL' . PHP_EOL);
    } else if($json['errorCode'] == Errors::INTEGRAL_ERR_SESSION_UNABLE_TO_LOGIN) {
        print('testLoginFail(): OK' . PHP_EOL);
    }
}

function testLogoutSuccess() {
    $dbconn = new DBConnector();
    $dbconn->connect();
    $mod = new SessionModule();
    if(!$mod->init($dbconn)) {
        print('testLogoutSuccess(): FAIL' . PHP_EOL);
        return;
    };

    $rs = $mod->login('admin', '73e1ddfb1e4c7ef386a0a9bcc6be34f0eec43dc5', 'localhost', 'none');
    $json = json_decode($rs, true);
    if((is_null($json)) ||
        ($json['errorCode'] != Errors::INTEGRAL_ERR_NONE)) {
        print('testLogoutSuccess(): FAIL' . PHP_EOL);
        return;
    }
    $rs = $mod->logout($json['data']['sessionKey']);
    $json = json_decode($rs, true);
    if((is_null($json)) ||
        ($json['errorCode'] != Errors::INTEGRAL_ERR_NONE)) {
        print('testLogoutSuccess(): FAIL' . PHP_EOL);
    } else {
        print('testLogoutSuccess(): OK' . PHP_EOL);
    }
}

testLoginSuccess();
testLoginFail();
testLogoutSuccess();
